/*
 * The PAC agent that adds story contribution functionality.
 * The
 */

import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class JoinStoryAgent implements Runnable {

	// The following variables hold the local model of the current story state, and collectively make up the abstraction component of this PAC agent.
	private final NetCommunicationWrapper conn;
	private final Scanner in;
	private String name;
	private final ArrayList<String> story;
	private String storyTitle;
	private String currentAuthor;

	JoinStoryAgent(NetCommunicationWrapper conn)
	{
		this.conn = conn;
		in = new Scanner(System.in);
		story = new ArrayList<String>();
		storyTitle = null;
	}

	/*
	 * This run() method is the control component of this PAC agent.
	 * When this agent is employed in this application, the Thread that instantiated this agent creates a separate Thread for this agent.
	 * This run() method is then called to control its execution.
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run()
	{

		setAuthorName();	//Retreive author name from user.

		while(Integer.parseInt(conn.receiveString()) != 6)
		{
			;	//Wait until the CSAClientCommunicator thread requests the author name.
		}
		conn.send(name);

		conn.send("1");	//Request story title
		storyTitle = conn.receiveString();

		while(true)
		{
			conn.send("2");	//Request up to date story.
			int numberOfLines = Integer.parseInt(conn.receiveString());
			for(int i = 0; i < numberOfLines; i++)
			{
				if(i < story.size()) {
					story.set(i,conn.receiveString());
				} else {
					story.add(conn.receiveString());
				}
			}

			displayStoryToDate();	//Display current story.

			conn.send("3");
			currentAuthor = conn.receiveString();
			if(currentAuthor.equals(name))
			{
				conn.send("4");
				conn.send(writeNextPart());	// Send the retrieved story addition.
			}
			else
			{
				displayCurrentAuthor();	// Display wait status.
				if(promptToLeave())	// Prompt the user to leave. If they would like to do so, inform the server and close the connection.
				{
					conn.send("5");
					conn.close();
					return;

				}
				conn.send("3");	//Request the current author again.
				while(conn.receiveString().equals(currentAuthor))	//Check to see if the current author has changed.
				{
					try {
						Thread.sleep(3000);
						conn.send("3");
					}
					catch (InterruptedException e) {
					}
				}
			}
		}

	}

	// Presentation Component method, part 1 of 5.
	// Prompts the user for their author name, then sets the variable.
	private void setAuthorName()
	{
		name = JOptionPane.showInputDialog(null,"Enter your name:");
	}

	// Presentation Component method, part 2 of 5.
	// Displays the current contents of story.
	private void displayStoryToDate()
	{
		String msg = "";
		for(int i = 0; i < story.size(); i++)
		{
			msg = msg + story.get(i) +"\n";
			//System.out.println(story.get(i));
		}

		JOptionPane.showMessageDialog(null, msg, "Participating in "+storyTitle, JOptionPane.PLAIN_MESSAGE);
	}

	// Presentation Component, part 3 of 5.
	// Prompts the user for the next story installment.
	private String writeNextPart() {
		String next ="";
		//		JPanel panel = new JPanel();
		//		JTextArea text = new JTextArea("", 2, 2);
		//		panel.add(text);
		next = JOptionPane.showInputDialog(null,"Enter the next part of the story");
		//System.out.println("Type the next part of the story: ");

		return next;
	}

	// Presentation Component method, part 4 of 5.
	// Displays the current writing author.
	private void displayCurrentAuthor()
	{
		JOptionPane.showMessageDialog(null, "Waiting on " + currentAuthor);
		//System.out.println("Waiting on " + currentAuthor);
	}

	// Presentation Component method, part 4 of 5.
	// Prompts the user if they would like to leave. Returns true if so.
	private boolean promptToLeave()
	{
		Object[] options = {"No, thanks!", "Yes, Please!"};
		int n = JOptionPane.showOptionDialog(null,"Would you like to leave?",
				"Exit?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
				null, options, options[1]);
		if(n == 1) {
			return true;
		}
		else {
			return false;
		}
	}

}
